package cn.com.dhc.danlu.activity.home.viewmodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.AlwaysBuyActivity;
import cn.com.dhc.danlu.activity.coupon.DealersCouponObtainActivity;
import cn.com.dhc.danlu.activity.delivery.order.MyOrdersActivity;
import cn.com.dhc.danlu.activity.home.wrap.CategoryEvent;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.widget.adapter.SelectTypeGridAdapter;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.dhc.danlu.widget.nested.ScrollGridView;
import cn.com.hd.mall.web.webservices.entity.response.common.GetTopicListResponse.TopicListModel;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * 首页中间选择视图
 *
 * @author wuhaiyang
 */
public class SelectTypeHeader extends LinearLayout {
    public static final int STABLE_COLUMS = 4;

    public static final String TAG = "SelectTypeHeader";

    public SelectTypeHeader(Context context) {
        this(context, null);
    }

    public SelectTypeHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectTypeHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        createGridView();
        setBackgroundColor(getResources().getColor(R.color.white));
    }

    private void createGridView() {
        final ScrollGridView gridView = new ScrollGridView(getContext());

        gridView.setNumColumns(STABLE_COLUMS);
        gridView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        gridView.setVerticalScrollBarEnabled(false);
        gridView.setVerticalFadingEdgeEnabled(false);
        gridView.setHorizontalScrollBarEnabled(false);
        gridView.setHorizontalFadingEdgeEnabled(false);
        gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gridView.setGravity(Gravity.CENTER);
        gridView.setSelector(android.R.color.transparent);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SelectTypeGridAdapter adapter = (SelectTypeGridAdapter) gridView.getAdapter();
                TopicListModel model = (TopicListModel) adapter.getItem(position);
                selectTypeLogic(model);
            }
        });
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(gridView, lp);
    }

    // 类型:T01-白酒，T02-葡萄酒，T03-啤酒，T04-其他饮品，T11-我的订单，T12-我的常购，T13-经销商红包，T14-优惠券
    private void selectTypeLogic(TopicListModel model) {
        String type = model.getTopicType();
        //暂时采用这种hardcode方式实现吧
        switch (type) {
            case "T01":
                //白酒
                screenWineType(CommonConst.IntentKey.WINE_CODE, getContext().getResources().getString(R.string.category_wine_label));
                break;
            case "T02":
                // 葡萄酒
                screenWineType(CommonConst.IntentKey.RED_WINE_CODE, getContext().getResources().getString(R.string.category_red_wine));
                break;
            case "T03":
                // 饮料
                screenWineType("C01X01", getContext().getResources().getString(R.string.category_drinks));
                break;
            case "T04":
                //副食
                screenWineType("C01F01", getContext().getResources().getString(R.string.category_non_staple));
                break;
            case "T11": {
                MyOrdersActivity.startSelf(getContext(), MyOrdersActivity.ALL_INDEX);
                break;
            }
            case "T12": { // 我的常购
                Intent intent = new Intent(getContext(), AlwaysBuyActivity.class);
                getContext().startActivity(intent);
                break;
            }
            case "T13": { // 跳转"抢经销商红包"
                Bundle data = new Bundle();
                data.putSerializable("type", FilterType.dealers_envelop);
                DealersCouponObtainActivity.startSelf(getContext(), data);
                break;
            }
            case "T14": { // 跳转"领券中心"
                Bundle data = new Bundle();
                data.putSerializable("type", FilterType.dealers_coupon);
                DealersCouponObtainActivity.startSelf(getContext(), data);
                break;
            }
        }
    }

    private void screenWineType(String code, String des) {
        agent.setFromTag(TAG);
        CategoryEvent event = new CategoryEvent(code, des);
        EventBus.getDefault().post(event);
    }

    public void refreshGridItem(List<TopicListModel> list) {
        SelectTypeGridAdapter adapter = new SelectTypeGridAdapter(getContext());
        GridView gridView = (GridView) getChildAt(0);
        gridView.setAdapter(adapter);
        adapter.refreshData(list);
    }

    public void refreshBadge(boolean isOrderbadge, int count) {
        GridView gridView = (GridView) getChildAt(0);
        if (null != gridView.getAdapter() && gridView.getAdapter() instanceof SelectTypeGridAdapter) {
            SelectTypeGridAdapter adapter = (SelectTypeGridAdapter) gridView.getAdapter();
            adapter.refreshBadge(isOrderbadge, count);
        }
    }
}